package com.web;

import com.function.domain.CallmeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@RestController
@RequestMapping("/producer")
public class ProviderController {

    AtomicInteger atomicInteger = new AtomicInteger(1);
    @Autowired
    StreamBridge streamBridge;

    @RequestMapping("{message}")
    public Boolean sendMsg(@PathVariable String message){
        return streamBridge.send("callmeEventSupplier-out-0", new CallmeEvent(atomicInteger.getAndIncrement(), message));
    }
    @RequestMapping("/{message}/process/{process}")
    public Boolean sendMsgWithHeaders(@PathVariable(name = "message") String message,@PathVariable(name = "process") Boolean process){
        CallmeEvent callmeEvent = new CallmeEvent(atomicInteger.getAndIncrement(), message);

        MessageHeaders headers = new MessageHeaders(new HashMap(){
            {
                put("to_process",process);
            }
        });
        Message<CallmeEvent> message1 = MessageBuilder.createMessage(callmeEvent, headers);

        return streamBridge.
                 send("callmeEventSupplier-out-0",
                         message1);
    }



}
